<template>
  <div class="login-container">
    <el-form
      ref="loginRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <!-- 背景图 -->
      <img src="@/assets/logo.png" alt="" class="login-logo">
      <!-- 用户名 -->
      <el-form-item prop="loginName">
        <span class="svg-container">
          <svg-icon icon-class="photo" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.loginName"
          placeholder="请输入账户"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>
      <!-- 密码 -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="jiesuo" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>
      <!-- 验证码 -->
      <el-form-item prop="code">
        <div class="verification">
          <div>
            <span class="svg-container">
              <svg-icon icon-class="dunpai" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.code"
              placeholder="请输入验证码"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
              maxlength="4"
            />
          </div>
          <!-- 随机数 + 请求验证码图片  src属性可以请求 -->
          <div @click.native="Random">
            <img :src="imgUrl + loginForm.clientToken" class="code-image">
          </div>
        </div>
      </el-form-item>
      <!-- 登录按钮 -->
      <el-button
        class="loginBtn"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
      >登录</el-button>
    </el-form>
  </div>
</template>

<script>
// import { ImageCode } from '@/api'
export default {
  name: 'Login',
  data() {
    return {
      // 图片验证码
      imgUrl:
        'http://likede2-admin.itheima.net/likede/api/user-service/user/imageCode/',
      loginForm: {
        loginName: 'admin', // 名
        password: 'admin', // 密码
        code: '', // 验证码
        loginType: 0, // 登录类型
        clientToken: '0'// 客户端请求验证码的token，两个后台登录需要提供
      },
      loginRules: {
        loginName: [
          { required: true, message: '请输入账户', trigger: 'change' },
          { min: 1, max: 18, message: '账号应在18个字符内', trigger: 'change' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          { min: 1, max: 18, message: '密码格式为1-20个字符', trigger: 'change' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'change' },
          { pattern: /^(?=.*[a-zA-Z0-9])(?=.*\d).+$/,
            message: '请输入图片中验证字段', trigger: 'change' }
        ]
      },
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  // // 请求验证码图片
  // async created() {
  //   const res = await ImageCode()
  //   console.log(res)
  // },
  methods: {
    // 生成随机数
    Random() {
      this.loginForm.clientToken = String(Math.floor(Math.random() * 100 + 1))
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 点击登录执行的方法
    handleLogin() {
      // 1. 表单兜底校验
      this.$refs.loginRef.validate(async(valid) => {
        if (valid) {
          // 2. 发起请求
          // 已经在store中actions方法里调用了登录接口并存储了token
          const res = await this.$store.dispatch('user/Signin', this.loginForm)
          console.log(res)
          this.$router.push('/')
        } else {
          console.log('校验失败')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
// 全局生效的样式
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #68b0fe;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #999;
      height: 47px;
      caret-color: #999;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    width: 100%;
    height: 52px;
    margin-bottom: 24px;
    background: #fff;
    border: 1px solid #e2e2e2;
    border-radius: 4px;
  }
  .verification {
    height: 52px;
  }

  .el-form-item__error {
    color: red;
  }
}
</style>

<style lang="scss" scoped>
// 只对当前组件生效的样式
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  position: relative;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background-image: url("~@/assets/background.png"); // 设置背景图片
  background-repeat: no-repeat;
  background-size: cover;
  .login-form {
   position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    backdrop-filter: blur(5px);
    // background: #fff;
    background-color: #fff;;
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
  }
  .loginBtn {
    width: 100%;
    height: 52px;
    background: linear-gradient(262deg,#2e50e1,#6878f0);
    opacity: .91;
    border-radius: 8px;
    color: #fff;
    text-shadow: 0 7px 22px #cfcfcf;
  }

  .tips {
    font-size: 14px;
    color: pink;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    .svg-icon{
      width: 16px;
      height: 16px;
    }
  }

// 验证码
  .verification{
    display: flex;
    -webkit-box-pack: justify;
    justify-content: space-between;
  }

  .login-logo {
    position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -48px;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
